var e = getApp(), o = require("../../utils/core.js");

Page({
    data: {
        shop_logo: "",
        shop_name: "",
        shop_code: "",
        shop_openid: "",
        shop_session_key: "",
        shop_phone: "",
    },
    onLoad: function(e) {
        var n = this;
        o.get("wxAppSetting", {}, function(e) {
            var o = e.sysset;
            n.setData({
                shop_logo: o.shoplogo,
                shop_name: o.shopname
            });
        });
    },
    onReady: function() {},
    onShow: function() {
        var that = this;
        var userinfo_phone = e.getCache("userinfo_phone");
        
        if(userinfo_phone){
            that.setData({
                shop_phone: userinfo_phone
            })
        }else{
            wx.login({
                success(res) {
                    var code = res.code;
                    o.post("wxapp/login", { code: code }, function(res) {
                        // console.log(res);
                        that.setData({
                            shop_session_key: res.session_key,
                            shop_openid: res.openid,
                        })
                    })
                }
            })
        }
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    getPhoneNumber: function(res) {
        let th = this, data = {};
        data = {
            encryptedData: encodeURIComponent(res.detail.encryptedData),
            iv: res.detail.iv,
            sessionKey: th.data.shop_session_key,
        }
        if (res.detail.errMsg === 'getPhoneNumber:ok') {
            o.post("wxapp/check_mobile", data, function(ret) {
                console.log(ret)
                if(ret.error == 0) e.setCache("userinfo_phone", ret.phoneNumber), th.setData({ shop_phone: ret.phoneNumber});
                else o.alert(ret.message);
            })
        } else {
            o.alert("获取用户手机号失败!");
        }
    },
    bindGetUserInfo: function(n) {
        wx.showLoading({
            title: "加载中"
        }), wx.login({
            success: function(i) {
                console.log([ 1, i ]), o.post("wxapp/login", {
                    code: i.code
                }, function(i) {
                    var mobile = e.getCache("userinfo_phone");
                    console.log([ 2, i ]), i.error ? o.alert("获取用户登录态失败:" + i.message) : o.get("wxapp/auth", {
                        data: n.detail.encryptedData,
                        iv: n.detail.iv,
                        sessionKey: i.session_key,
                        mobile: mobile
                    }, function(s) {
                        console.log([ 3, s ]), 1 == s.isblack && wx.showModal({
                            title: "无法访问",
                            content: "您在商城的黑名单中，无权访问！",
                            success: function(o) {
                                o.confirm && e.close(), o.cancel && e.close();
                            }
                        }), n.detail.userInfo.openid = s.openId, n.detail.userInfo.id = s.id, n.detail.userInfo.uniacid = s.uniacid;
                        var t = e.setCache("userinfo", n.detail.userInfo), a = e.setCache("userinfo_openid", n.detail.userInfo.openid), c = e.setCache("userinfo_id", s.id);
                        console.log(e.getCache("userinfo")), console.log(e.getCache("userinfo_openid")), 
                        console.log(e.getCache("userinfo_id"));
                        var l = e.setCache("login_session_key", i.session_key);
                        0 != t && 0 != a && 0 != c && 0 != l || o.alert("获取用户信息失败!"), console.log(e.getCache("login_session_key")), 
                        e.getSet(), e.scanCarts(), wx.navigateBack({
                            changed: !0
                        });
                    });
                });
            },
            fail: function() {
                o.alert("获取用户信息失败!");
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    navigateBack: function() {
        wx.navigateBack({
            changed: !0
        });
    },
    close: function() {
        wx.navigateBack({
            delta: 0
        });
    },
    getUserProfile: function() {
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                wx.showLoading({
                    title: "加载中"
                }), wx.login({
                    success: function(t) {
                        o.post("wxapp/login", {
                            code: t.code
                        }, function(t) {
                            var mobile = e.getCache("userinfo_phone");
                            e.setCache("userinfo_openid",t.openid);
                            t.error ? o.alert("获取用户登录态失败:" + t.message) : o.get("wxapp/auth", {
                                data: encodeURIComponent(res.encryptedData),
                                iv: res.iv,
                                sessionKey: t.session_key,
                                mobile: mobile,
                            }, function(n) {
                                1 == n.isblack && wx.showModal({
                                    title: "无法访问",
                                    content: "您在商城的黑名单中，无权访问！",
                                    success: function(n) {
                                        n.confirm && e.close(), n.cancel && e.close();
                                    }
                                }), res.userInfo.openid = t.openid, res.userInfo.id = n.id, res.userInfo.uniacid = n.uniacid, 
                                e.setCache("userinfo", res.userInfo), e.setCache("userinfo_openid", t.openid), 
                                e.setCache("userinfo_id", n.id), e.getSet();
                                wx.navigateBack({
                                    changed: !0
                                });
                            });
                        });
                    },
                    fail: function() {
                        o.alert("获取用户信息失败!");
                    },
                    complete: function() {
                        wx.hideLoading();
                    }
                });
            }
        })
    }
});